#!/home/lin/software/miniconda3/envs/th1.11/bin/python
# -*- coding: utf-8 -*-
import open3d as o3d
import numpy as np
import math

'''
Open3d的数据结构天生的支持NumPy.
首先我们生成一个n × 3的矩阵xyz,,每一列的x,y,z都由一个函数 z = sin(x^2 + y^2)/(x^2 + y^2);z_norm是z在[0,1]区间的归一化映射.
'''
# x = np.linspace(-2, 2, 500)
# mesh_x, mesh_y = np.meshgrid(x, x)  #np.meshgrid从坐标向量中返回坐标矩阵, 这里产生100*100个坐标,并赋值给mesh_x，mesh_y
#
# z = np.sinc((np.power(mesh_x, 2) + np.power(mesh_y, 2)))
# z_norm = (z - z.min()) / (z.max() - z.min())
# xyz = np.zeros((np.size(mesh_x), 3))
# xyz[:, 0] = np.reshape(mesh_x, -1)
# xyz[:, 1] = np.reshape(mesh_y, -1)
# xyz[:, 2] = np.reshape(z_norm, -1)
# print('xyz', xyz.shape)

# -------------------
# x=a*(2*cos(t)-cos(2*t))y=a*(2*sin(t)-sin(2*t))
t = np.linspace(0, 2 * math.pi, 300)
a = 10
x = a * (2 * np.cos(t) - np.cos(2 * t))
y = a * (2 * np.sin(t) - np.sin(2 * t))
z = np.ones(t.shape[0])
xyz = np.zeros((np.size(t), 3))
xyz[:, 0] = np.reshape(x, -1)
xyz[:, 1] = np.reshape(y, -1)
xyz[:, 2] = np.reshape(z, -1)
print('xyz', xyz.shape)

# 从NumPy转为open3d.PointCloud
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)
# o3d.io.write_point_cloud("../../TestData/sync.ply", pcd)
pcd.paint_uniform_color((1, 0, 0))   # rgb
o3d.visualization.draw_geometries([pcd])

# 从open3d.PointCloud转为NumPy, 直接使用np.asarray(pcd.points)
pcd_load = o3d.io.read_point_cloud("../data/bun0.pcd")

# convert Open3D.o3d.geometry.PointCloud to numpy array
xyz_load = np.asarray(pcd_load.points)
print('xyz_load')
print(xyz_load.shape)
o3d.visualization.draw_geometries([pcd_load])